package org.apache.catalina.util;

import org.apache.catalina.Lifecycle;
import org.apache.catalina.LifecycleEvent;
import org.apache.catalina.LifecycleListener;

/**
 * 2020/7/22 : 14:50
 */
public final class LifecycleSupport {
    
    private Lifecycle lifecycle;
    private LifecycleListener[] listeners = new LifecycleListener[];
    
    public LifecycleSupport(Lifecycle lifecycle){
        this.lifecycle=lifecycle;    
    }

    /**
     * 激活生命周期事件
     * @param type
     * @param data
     */
    public void fireLifecycleEvent(String type, Object data) {
        LifecycleEvent event = new LifecycleEvent(lifecycle,type,data);
        LifecycleListener[] lifecycleListeners = null;
        synchronized (lifecycleListeners){
            lifecycleListeners=listeners.clone();
        }
        for (int i = 0; i < lifecycleListeners.length; i++) {
            lifecycleListeners[i].lifecycleEvent(event);
        }
    }
}
